package com.wasion.gydpe.common.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;

import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @ClassName: JxlsUtil
 * @Description: JXLS工具类
 * @author 高意
 * @date Oct 17, 2012 3:17:18 PM
 * 
 */
public class JxlsUtil {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(JxlsUtil.class);

	/**
	 * 
	 * @Title: exportExcel
	 * @Description: 导出Excel工具方法
	 * @param destFileName：生成文件名；templateFilePath：模板文件路径；datumMap：待导出数据
	 * @return
	 * @throws
	 */
	public static void exportExcel(String destFileName,
			String templateFilePath, Map<String, Object> datumMap,
			HttpServletResponse response) throws ParsePropertyException,
			IOException {

		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Disposition", "attachment; filename="
				+ destFileName);

		try {
			InputStream inputStream = new BufferedInputStream(
					new FileInputStream(templateFilePath));

			// 生成表单，并将表单写入输出流
			XLSTransformer transformer = new XLSTransformer();
			Workbook workbook = transformer.transformXLS(inputStream, datumMap);

			OutputStream outputStream = response.getOutputStream();
			workbook.write(outputStream);
			inputStream.close();

			outputStream.flush();
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.error("导出Excle失败：exportExcel >>>>>> " + e);
		}
	}
}
